package com.ptszyxx.popose.module.main.fish;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YVipUtil;
import com.ptszyxx.popose.databinding.FragmentFishBinding;
import com.ptszyxx.popose.module.main.fish.adapter.FishAdapter;
import com.ptszyxx.popose.module.main.fish.vm.FishVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.home.HomeResult;
import java.util.List;

public class FishFragment extends BaseFragment<FragmentFishBinding, FishVM> {
    private FishAdapter adapter;
    private boolean statusBarFlag = true;

    public FishFragment(){
    }

    public FishFragment(boolean statusBarFlag){
        this.statusBarFlag = statusBarFlag;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_fish;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FishVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FishVM.class);
    }

    @Override
    public void initStatusBar() {
        if (statusBarFlag){
            super.initStatusBar();
        }
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new FishAdapter(viewModel);
        binding.refreshView.initAdapterGrid(adapter, 4);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestCity();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onCityEvent.observe(this, o -> setAdapterUI((List<HomeResult>)o));
        viewModel.uc.onDetailEvent.observe(this, o -> onJumpDetail((String)o));
    }

    private void onJumpDetail(String userId) {
        if (YVipUtil.isVip(getContext(), true)){
            YActivityUtil.getInstance().jumpUserDetail(viewModel, userId);
        }
    }

    private void setAdapterUI(List<HomeResult> list) {
        binding.refreshView.setAdapterUI(adapter, list, true);
    }

}
